﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;


namespace MO
{
    public class DataManager
    {
        public static List<EntityDefine> FindAllEntity()
        {
            DataTable table = DbOperation.DB.ExecuteDataTable("select * from Entity_Define");
            return PackagingList<EntityDefine>(table);
        }
        public static EntityDefine FindEntityByEntityCode(string code)
        {
            DataTable table = DbOperation.DB.ExecuteDataTable("select * from Entity_Define where ED_Code={0}", code);
            var list = PackagingList<EntityDefine>(table);
            if (list.Count > 0)
                return list[0];
            return null;
        }
        public static List<FieldDefine> FindFieldByEntityCode(string code)
        {
            DataTable table = DbOperation.DB.ExecuteDataTable("select * from Field_Define where ED_Code={0} order by FD_Order", code);
            return PackagingList<FieldDefine>(table);
        }
        public static FieldDefine FindFieldByFieldCode(string code)
        {
            DataTable table = DbOperation.DB.ExecuteDataTable("select * from Field_Define where FD_Code={0}", code);
            var list = PackagingList<FieldDefine>(table);
            if (list.Count > 0)
                return list[0];
            return null;
        }
        public static FormDefine FindFormByFormCode(string code)
        {
            DataTable table = DbOperation.DB.ExecuteDataTable("select * from Form_Define where Form_Code={0}", code);
            var list = PackagingList<FormDefine>(table);
            if (list.Count > 0)
                return list[0];
            return null;
        }
        public static List<FormItem> FindFormItemByFormCode(string code)
        {
            DataTable table = DbOperation.DB.ExecuteDataTable("select * from Form_Item where Form_Code={0} order by FI_Order", code);
            return PackagingList<FormItem>(table);
        }
        public static ListDefine FindListByListCode(string code)
        {
            DataTable table = DbOperation.DB.ExecuteDataTable("select * from List_Define where LD_Code={0}", code);
            var list = PackagingList<ListDefine>(table);
            if (list.Count > 0)
                return list[0];
            return null;
        }
        public static Dictionary<string, object> FindDataByTableCode(string tableName,string code)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            DataTable table = DbOperation.DB.ExecuteDataTable("select * from " + tableName + " where " + tableName + "_CODE={0} ", code);
            foreach (DataRow row in table.Rows)
            {
                foreach (DataColumn col in table.Columns)
                {
                    result[col.ColumnName] = row[col];
                }
            }
            return result;
        }
        static List<T> PackagingList<T>(DataTable table)
        {
            List<T> result = new List<T>();
            foreach (DataRow row in table.Rows)
            {
                T item = (T)Activator.CreateInstance(typeof(T), row);
                result.Add(item);
            }
            return result;
        }
    }






}
